<?php
require_once (APPPATH.'views/components/page.php');
require_once (APPPATH.'models/specialmark_model.php');
require_once (APPPATH.'models/branch_model.php');
require_once (APPPATH.'libraries/stringutils.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<link href='<?php echo base_url() ?>resources/css/admin.css' rel='stylesheet' type='text/css' />
		<script src="<?php echo base_url() ?>resources/js/jquery-1.4.2.min.js"></script>
		<script src="<?php echo base_url() ?>resources/js/ckeditor/ckeditor.js"></script>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		
		<script type="text/javascript">
			function onSaveBtnClicked()
			{
				var ret = true;
				
				// Title
				var title = document.getElementById("title").value;
				title = title.trim();
				
				if (title.length == 0)
				{
					var titleError = document.getElementById("titleerror");
					titleError.innerHTML = "\t\t\t Vui lòng nhập tiêu đề";
					ret = false;
				}
				
				// Image
				var image = document.getElementById("image").value;
				image = image.trim();
				
				if (image.length == 0)
				{
					var imageError = document.getElementById("imageerror");
					imageError.innerHTML = "\t\t\t Vui lòng nhập hình ảnh";
					ret = false;
				}
				
				// Content
				var content = CKEDITOR.instances.newscontent.getData();
				content = content.trim();
				
				if (content.length == 0)
				{
					var contentError = document.getElementById("contenterror");
					contentError.innerHTML = "\t\t\t Vui lòng nhập nội dung";
					ret = false;
				}
				
				return ret;
			}
			
			function onCancelBtnClicked(baseurl)
			{
				window.location = baseurl + "news";
			}
			
			function isNumericKey(e)
		    {
		        if (window.event) { var charCode = window.event.keyCode; }
		        else if (e) { var charCode = e.which; }
		        else { return true; }
		        if (charCode > 31 && (charCode < 48 || charCode > 57)) { return false; }
		        return true;
		    }
		</script>
		
		<?php
			$title = isset($news) ? "Cập nhật tin tức" : "Thêm tin tức";
		?>
		
		<title><?php echo $title ?></title>
	</head>
	
	<body>
		<div id='wrapper'>
			<div id='header'>
				<div id="leftTopPan">
				<a href='<?php echo base_url() ?>'><img src='<?php echo base_url() ?>resources/images/logo.gif' title='Consultant' alt='Consultant' width='160' height='39' border='0' /></a>
				</div>
				<h1></h1>
			</div>
			<div id="middle">
				<div id="container">
					<div id="content">
						<br /><h2 align="center"><?php echo $title ?></h2><br />
						
						<?php
						
						// Error handler
						$error = $this->session->userdata('error');
						$this->session->unset_userdata('error');
						
						if (isset($rerror))
							$error = $rerror;
						
						$errorText = "";
						
						switch ($error) {
							case UPDATE_ERROR:
								$errorText = UPDATE_ERROR_TEXT;
								break;
							case EXISTED_ERROR:
								$name = $this->session->userdata('productname');
								$this->session->unset_userdata('productname');
								$productType = $this->session->userdata('producttypename');
								$this->session->unset_userdata('producttypename');
								$errorText = sprintf(PRODUCT_EXISTED_ERROR_TEXT, "Sản phẩm", $name, $productType);
								break;
							case INSERT_ERROR:
								$error = sprintf(INSERT_ERROR_TEXT, "Sản phẩm");
								break;
							case NOT_EXISTED_ERROR:
								$errorText = NOT_EXISTED_ERROR_TEXT;
								break;
							default:
								break;
						}
						?>
						<h3 style="color: red"><?php echo $errorText ?></h3>
						<?php
						
						
						$userdata = $this->session->userdata('userdata');
						
						if (Page::getInstance()->isAdmin())
						{
							if (isset($news))
							{
								if ($news->getId() < 0)
									return;
							}
							// User information
							Page::getInstance()->generateUserInfo($userdata);
							
							// Begin form
							
							echo form_open('news/processinsert');
							
							// Default values
							$id = -1;
							$title = "";
							$content = "";
							$img = "";
							$promotion = 0;
							
							if (isset($news))
							{
								$id = $news->getId();
								$title = $news->getTitle();
								$content = $news->getContent();
								$img = $news->getImage();
								$promotion = $news->isPromotion();
							}
							
							// Hidden form
							echo form_hidden('newsid', $id);
							
							// Text box
							$titleTxt = array(	'id'			=>	'title',
												'name'			=>	'title',
												'value'			=>	$title);
							$image = array(	'id'	=>	'image',
											'name'	=>	'image',
											'size'	=>	60,
											'value'	=>	$img);
							$checkbox = array(	'id'	=>	'promotion',
												'name'	=>	'promotion',
												'checked'=>	$promotion,
												'value'	=>	$promotion);
						?>
							<p>Tiêu đề: 
							<?php echo form_input($titleTxt) ?>
							<label id='titleerror' style='color:red'></label></p>
							
							<p>Hình ảnh đại diện: <?php echo form_input($image) ?><label id='imageerror' style='color:red'></label></p>
							<p>Tin khuyến mãi: <?php echo form_checkbox($checkbox) ?></p>
						
							<p>Nội dung:<label id='contenterror' style='color:red'></label></p>
							<textarea cols="80" id="newscontent" name="newscontent" rows="20" ><?php echo $content ?></textarea>
							<script type="text/javascript">
							//<![CDATA[
					
								// Replace the <textarea id="editor"> with an CKEditor
								// instance, using default configurations.
								CKEDITOR.replace( 'newscontent',
									{
										toolbar :
										[
											[ 'Format', 'Font','FontSize', 'Strike', 'Underline', 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink', 
											'-', 'TextColor', '-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl', 'Image']
										]
									});
					
							//]]>
							</script>
							<?php
							
							echo "<br/>";
							// Save button
							$saveBtn = array(	'id'		=>	'savebtn',
												'name'		=> 	'savebtn',
												'value'		=>	'Lưu',
												'onclick'	=>	'return onSaveBtnClicked()');
							echo form_submit($saveBtn);
							
							// Reset button
							$cancelBtn = array(	'id'		=> 'cancelbtn',
												'name'		=> 'cancelbtn',
												'value'		=> 'Hủy',
												'onclick'	=> "onCancelBtnClicked('".base_url()."')");
							echo form_reset($cancelBtn);
							
							// End insert form
							echo form_close();
						}
						?>
					</div>
				</div>
					<?php
						if (Page::getInstance()->isAdmin())
						{
							Page::getInstance()->generateAdminLeftMenu();
						}
					?>
			</div>
		</div>
		
	</body>
</html>